import {httpResult} from "../utils/utils.js";
import blockchainPriceDao from "../Dao/blockchain_price.js";

// 添加币种监听 http://127.0.0.1:52000/blockchain/addCoin?name=BNB&unicode=bnb-usdt
// 添加ETH币种监听 http://127.0.0.1:52000/blockchain/addCoin?name=ETH&unicode=eth-usdt
// 添加ETH币种监听 http://127.0.0.1:52000/blockchain/addCoin?name=BTC&unicode=btc-usdt
// 添加ETH币种监听 http://127.0.0.1:52000/blockchain/addCoin?name=MATIC&unicode=matic-usdt
// 添加ETH币种监听 http://127.0.0.1:52000/blockchain/addCoin?name=SOL&unicode=sol-usdt
// 添加ETH币种监听 http://127.0.0.1:52000/blockchain/addCoin?name=ORDI&unicode=ordi-usdt
// 添加ETH币种监听 http://127.0.0.1:52000/blockchain/addCoin?name=DOGE&unicode=doge-usdt
// 添加ETH币种监听 http://127.0.0.1:52000/blockchain/addCoin?name=OKX&unicode=okb-usdt
// 添加ETH币种监听 http://127.0.0.1:52000/blockchain/addCoin?name=USDT&unicode=buy-usdt&platform=https://www.shuidingding.com/zh-hans/p2p-markets/cny/&selector=.price
// 添加币种监听 http://124.222.220.135:52000/blockchain/addCoin?name=BNB&unicode=bnb-usdt
// 添加ETH币种监听 http://124.222.220.135:52000/blockchain/addCoin?name=ETH&unicode=eth-usdt
// 添加ETH币种监听 http://124.222.220.135:52000/blockchain/addCoin?name=BTC&unicode=btc-usdt
// 添加ETH币种监听 http://124.222.220.135:52000/blockchain/addCoin?name=MATIC&unicode=matic-usdt
// 添加ETH币种监听 http://124.222.220.135:52000/blockchain/addCoin?name=SOL&unicode=sol-usdt
// 添加ETH币种监听 http://124.222.220.135:52000/blockchain/addCoin?name=ORDI&unicode=ordi-usdt
// 添加ETH币种监听 http://124.222.220.135:52000/blockchain/addCoin?name=DOGE&unicode=doge-usdt
// 添加ETH币种监听 http://124.222.220.135:52000/blockchain/addCoin?name=OKX&unicode=okb-usdt
// 添加ETH币种监听 http://124.222.220.135:52000/blockchain/addCoin?name=USDT&unicode=buy-usdt&platform=https://www.shuidingding.com/zh-hans/p2p-markets/cny/&selector=.price
export const addCoin = async (req, res) => {
  let {name, unicode, platform, selector} = req.query;
  console.log('addCoin', name, unicode, platform, selector, 'addCoin');
  if (!name || !unicode) {
    throw new Error('参数错误');
  }
  if(!platform) {
    platform = 'https://www.shuidingding.com/zh-hans/trade-spot/';
  }
  if(!selector) {
    selector = '#root > div > div.trade-header-box > div > div.header-left > div > div.ticker-last-box > span.last';
  }
  // 查询数据库中是否已经存在该币种
  let coin = await blockchainPriceDao.findByNameOrUnicode(name, unicode);
  if (coin) {
    throw new Error('该币种已存在');
  }
  // 添加数据进入数据库
  await blockchainPriceDao.create(0, name, unicode, platform, selector);
  res.json(httpResult.success({
    result: true
  }));
};
// 获取币种价格 http://127.0.0.1:52000/blockchain/getCoinPrice?name=BNB
// 获取币种价格 http://124.222.220.135:52000/blockchain/getCoinPrice?name=BNB
export const getCoinPrice = async (req, res) => {
  let {name} = req.query;
  if (!name) {
    throw new Error('参数错误');
  }
  // 查询数据库中是否已经存在该币种
  let coin = await blockchainPriceDao.findByName(name);
  if (!coin) {
    throw new Error('该币种不存在');
  }
  res.json(httpResult.success({
    result: true,
    name,
    price: coin.price
  }));
};
